require 'slam.slam'

local cache = {}
local maxSourcesPlaying = 2

local function cacheAudio(filename)
  if filename then
    if not cache[filename] then
      cache[filename] = love.audio.newSource(filename, 'static')
    end
    return cache[filename]
  end
end

function cache.play(filename)
  local audio = cacheAudio(filename)
  if audio then
    if love.audio.getNumSources() > maxSourcesPlaying then
      audio:stop()
    end
    audio:play()
  end
end

return cache
